﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Fc
{
    public partial class 备件到货发货登记 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            int id = 0;

            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.BuMen.DataBind();
                this.BuMen.Enabled = false;
                if ((object)Session["OperatorDCode"] != null)
                {
                    string dcode = (string)Session["OperatorDCode"];
                    if (this.BuMen.Items.FindByValue(dcode) != null)
                    {
                        this.BuMen.Text = dcode;
                    }
                    else
                    {
                        this.Button_Save.Enabled = false;
                    }
                }
                else
                {
                    this.Button_Save.Enabled = false;
                }

                string io = Request.QueryString["io"];
                if (io != null && io == "out")
                {
                    this.DaoFa.Text = "发货";
                }

                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }

                if (id > 0)
                {
                    this.DFID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.DFID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    DateTime d = DateTime.Today;
                    this.Date1.Text = d.ToLongDateString();
                }
            }
            this.Label_GuiGe.Text = this.GuiGe.Value;
            this.Label_DanWei.Text = this.DanWei.Value;
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                this.BPID.Value = ((int)e.Command.Parameters[1].Value).ToString();
                this.BuMen.Text = (string)e.Command.Parameters[2].Value;
                this.JieCun.Text = ((int)e.Command.Parameters[3].Value).ToString();
                this.Date1.Text = ((DateTime)e.Command.Parameters[4].Value).ToLongDateString();
                int shuliang = (int)e.Command.Parameters[5].Value;
                if (shuliang < 0)
                {
                    this.DaoFa.Text = "发货";
                    shuliang = -shuliang;
                }
                this.ShuLiang.Text = shuliang.ToString();

                this.DanJia.Text = ((double)e.Command.Parameters[6].Value).ToString();
                this.ChangShang.Text = (string)e.Command.Parameters[7].Value;
                this.JingBan.Text = (string)e.Command.Parameters[8].Value;
                this.BaoGuan.Text = (string)e.Command.Parameters[9].Value;
                this.BeiZhu.Text = (string)e.Command.Parameters[10].Value;
                this.PinMing.Text = (string)e.Command.Parameters[11].Value;
                this.GuiGe.Value = (string)e.Command.Parameters[12].Value;
                this.DanWei.Value = (string)e.Command.Parameters[13].Value;
                this.Label_GuiGe.Text = this.GuiGe.Value;
                this.Label_DanWei.Text = this.DanWei.Value;

                this.BuMen.Enabled = false;
                this.PinMing.Enabled = false;
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (this.BPID.Value == "0")
            {
                this.Message.Text = "·备品备件品种录入有误";
                return; 
            }

            int shuliang;
            if (!int.TryParse(this.ShuLiang.Text, out shuliang) || shuliang <= 0)
            {
                this.Message.Text = "·“数量”录入有误，请录入一个正整数";
                return; 
            }
            if (this.DaoFa.Text == "发货")
            {
                shuliang = -shuliang;
            }
            decimal danjia;
            if (!decimal.TryParse(this.DanJia.Text, out danjia) || danjia <= 0)
            {
                this.Message.Text = "·“单价”录入有误";
                return; 
            }

            DateTime d1;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                this.Message.Text = "·“日期”录入有误";
                return; 
            }
            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }
            //开始保存数据
            if (this.DFID.Text =="0")
            {
                ParameterCollection pc = this.SqlDataSource1.InsertParameters;
                pc["部门"].DefaultValue = this.BuMen.Text;
                pc["类别ID"].DefaultValue = this.BPID.Value;
                pc["日期"].DefaultValue = d1.ToShortDateString();
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                pc["厂商"].DefaultValue = this.ChangShang.Text;
                pc["经办人"].DefaultValue = this.JingBan.Text;
                pc["保管员"].DefaultValue = op;
                pc["备注"].DefaultValue = this.BeiZhu.Text;

                if (this.SqlDataSource1.Insert() > 0)
                {
                    this.Message.Text = "·保存成功";
                    this.Button_ReNew.Visible = true;
                    this.BuMen.Enabled = false;
                    this.PinMing.Enabled = false;
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
            else
            {
                ParameterCollection pc = this.SqlDataSource1.UpdateParameters;
                pc["ID"].DefaultValue = this.DFID.Text;
                pc["部门"].DefaultValue = this.BuMen.Text;
                pc["类别ID"].DefaultValue = this.BPID.Value;
                pc["日期"].DefaultValue = d1.ToShortDateString();
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                pc["厂商"].DefaultValue = this.ChangShang.Text;
                pc["经办人"].DefaultValue = this.JingBan.Text;
                pc["保管员"].DefaultValue = op;
                pc["备注"].DefaultValue = this.BeiZhu.Text;

                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[0].Value))
            {
                this.DFID.Text = ((int)e.Command.Parameters[0].Value).ToString();
                this.JieCun.Text = ((int)e.Command.Parameters[3].Value).ToString();
            }
        }

        protected void Button_ReNew_Click(object sender, EventArgs e)
        {
            if (this.DaoFa.Text == "发货")
            {
                Response.Redirect("备件到货发货登记.aspx?io=out");
            }
            else
            {
                Response.Redirect("备件到货发货登记.aspx");
            }
        }

        protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
            {
                this.JieCun.Text = ((int)e.Command.Parameters[3].Value).ToString();
            }
        }
    }
}
